home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 42
/
Amiga Format AFCD42 (Issue 126, Aug 1999).iso
/
-serious-
/
programming
/
other
/
jikes
/
src
/
lookup.h
< prev
next >
Wrap
C/C++ Source or Header
|
1999-05-14
|
16KB
|
747 lines
// $Id: lookup.h,v 1.5 1999/02/12 14:39:13 shields Exp $
//
// This software is subject to the terms of the IBM Jikes Compiler
// License Agreement available at the following URL:
// http://www.ibm.com/research/jikes.
// Copyright (C) 1996, 1998, International Business Machines Corporation
// and others. All Rights Reserved.
// You must accept the terms of that agreement to use this software.
//
#ifndef lookup_INCLUDED
#define lookup_INCLUDED
#include "config.h"
#ifndef __amigaos__
#include <wchar.h>
#endif
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include "tuple.h"
#include "long.h"
#include "double.h"
class Symbol;
class PackageSymbol;
class TypeSymbol;
class MethodSymbol;
class MethodShadowSymbol;
class BlockSymbol;
class VariableSymbol;
class VariableShadowSymbol;
class LabelSymbol;
class LiteralSymbol;
class NameSymbol;
class PathSymbol;
class DirectorySymbol;
class FileSymbol;
class ShadowSymbol;
class LiteralValue;
class IntLiteralValue;
class LongLiteralValue;
class FloatLiteralValue;
class DoubleLiteralValue;
class Utf8LiteralValue;
class Utf8LiteralTable;
class NameLookupTable;
class LiteralLookupTable;
class AstBinaryExpression;
class AstExpression;
class Hash
{
public:
//
// HASH takes as argument a pointer to a character string
// and its length which it hashes it into a location in the name
// hash table.
//
inline static unsigned Function(wchar_t *head, int len)
{
unsigned long hash_value = head[len >> 1]; // start with center (or unique) letter
wchar_t *tail = &head[len - 1];
for (int i = 0; i < 5 && head < tail; i++)
{
unsigned k = *tail--;
hash_value += ((k << 7) + *head++);
}
return hash_value;
}
//
// Same as above function for a regular "char" string.
//
inline static unsigned Function(char *head, int len)
{
unsigned long hash_value = head[len >> 1]; // start with center (or unique) letter
char *tail = &head[len - 1];
for (int i = 0; i < 5 && head < tail; i++)
{
unsigned k = *tail--;
hash_value += ((k << 7) + *head++);
}
return hash_value;
}
inline static unsigned Function(IEEEfloat value)
{
return value.Word();
}
inline static unsigned Function(IEEEdouble value)
{
unsigned result = value.HighWord() + value.LowWord();
return result;
}
};
class DirectoryEntry
{
public:
DirectoryEntry *next;
int length;
char *name;
DirectoryEntry() : name(NULL),
directory(NULL),
next(NULL),
length(0),
mtime_(0)
{
image = this;
}
virtual ~DirectoryEntry()
{
delete [] name;
}
inline void Initialize(DirectorySymbol *directory_, char *name_, int length_)
{
directory = directory_;
length = length_;
name = new char[length + 1];
memmove(name, name_, length * sizeof(char));
name[length] = U_NULL;
return;
}
inline void Initialize(DirectoryEntry *entry, char *name_, int length_)
{
Initialize(entry -> directory, name_, length_);
}
time_t Mtime();
bool IsDummy() { return this != image; }
//
// See FoldedDirectoryEntry for an explanation of the use of this function
//
virtual DirectoryEntry *Image() { return this; }
protected:
DirectorySymbol *directory;
DirectoryEntry *image;
time_t mtime_;
};
#ifdef WIN32_FILE_SYSTEM
//
// This object is needed only for systems such as Windows NT/95/98 that
// treat filenames in a case-insensitive fashion.
//
class FoldedDirectoryEntry : public DirectoryEntry
{
public:
FoldedDirectoryEntry(DirectoryEntry *image_) { DirectoryEntry::image = image_; }
virtual ~FoldedDirectoryEntry() {}
virtual DirectoryEntry *Image() { return image; }
};
#endif
class DirectoryTable
{
public:
Tuple<DirectoryEntry *> entry_pool;
DirectoryTable(int estimate = 1024);
~DirectoryTable();
DirectoryEntry *FindEntry(char *, int);
DirectoryEntry *InsertEntry(DirectorySymbol *, char *, int);
#ifdef WIN32_FILE_SYSTEM
//
// See FoldedDirectoryEntry for an explanation of the use of this function
//
DirectoryEntry *FindCaseInsensitiveEntry(char *, int);
void InsertCaseInsensitiveEntry(DirectoryEntry *);
#endif
private:
enum
{
DEFAULT_HASH_SIZE = 1021,
MAX_HASH_SIZE = 8191
};
DirectoryEntry **base;
int hash_size;
static int primes[];
int prime_index;
inline static unsigned Hash(char *head, int len) { return Hash::Function(head, len); }
void Rehash();
};
class Symbol
{
public:
Symbol *next;
enum SymbolKind
{
NONE,
NAME,
PACKAGE,
TYPE, // class or interface
METHOD,
BLOCK,
VARIABLE,
LABEL,
LITERAL,
PATH,
_DIRECTORY,
_FILE,
_num_kinds
};
SymbolKind Kind() { return _kind; }
virtual wchar_t *Name() { return (wchar_t *) NULL; }
virtual int NameLength() { return 0; }
virtual NameSymbol *Identity() { return (NameSymbol *) NULL; }
PackageSymbol *PackageCast() { return (PackageSymbol *) (_kind == PACKAGE ? this : NULL); }
TypeSymbol *TypeCast() { return (TypeSymbol *) (_kind == TYPE ? this : NULL); }
MethodSymbol *MethodCast() { return (MethodSymbol *) (_kind == METHOD ? this : NULL); }
BlockSymbol *BlockCast() { return (BlockSymbol *) (_kind == BLOCK ? this : NULL); }
VariableSymbol *VariableCast() { return (VariableSymbol *) (_kind == VARIABLE ? this : NULL); }
LabelSymbol *LabelCast() { return (LabelSymbol *) (_kind == LABEL ? this : NULL); }
LiteralSymbol *LiteralCast() { return (LiteralSymbol *) (_kind == LITERAL ? this : NULL); }
NameSymbol *NameCast() { return (NameSymbol *) (_kind == NAME ? this : NULL); }
PathSymbol *PathCast() { return (PathSymbol *) (_kind == PATH ? this : NULL); }
DirectorySymbol *DirectoryCast() { return (DirectorySymbol *) (_kind == _DIRECTORY ? this : NULL); }
FileSymbol *FileCast() { return (FileSymbol *) (_kind == _FILE ? this : NULL); }
virtual ~Symbol() {}
protected:
SymbolKind _kind;
};
class LiteralValue
{
public:
LiteralValue *next;
int constant_pool_index,
constant_pool_class;
virtual ~LiteralValue() {}
};
class IntLiteralValue : public LiteralValue
{
public:
int value;
virtual ~IntLiteralValue() {}
void Initialize(int value_)
{
value = value_;
LiteralValue::constant_pool_index = 0;
LiteralValue::constant_pool_class = 0;
}
};
class LongLiteralValue : public LiteralValue
{
public:
LongInt value;
virtual ~LongLiteralValue() {}
void Initialize(LongInt value_)
{
value = value_;
LiteralValue::constant_pool_index = 0;
LiteralValue::constant_pool_class = 0;
}
};
class FloatLiteralValue : public LiteralValue
{
public:
IEEEfloat value;
virtual ~FloatLiteralValue() {}
void Initialize(IEEEfloat value_)
{
value = value_;
LiteralValue::constant_pool_index = 0;
LiteralValue::constant_pool_class = 0;
}
};
class DoubleLiteralValue : public LiteralValue
{
public:
IEEEdouble value;
virtual ~DoubleLiteralValue() {}
void Initialize(IEEEdouble value_)
{
value = value_;
LiteralValue::constant_pool_index = 0;
LiteralValue::constant_pool_class = 0;
}
};
class Utf8LiteralValue : public LiteralValue
{
public:
char *value;
int length;
int constant_pool_index_String, // index when used as String
constant_pool_index_Class; // index when used as Class
Utf8LiteralValue() : value(NULL)
{}